<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>客户端信息</title>
<link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
<link rel="stylesheet" href="/layui/css/layui.css" media="all" />
</head>
<body>
    <div>
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <div>
                <header style="height: 100%">
                    <div align="left">
	                    <table style="width: 100%">
	                   		<tr>
	                   			<td>
		                   			<form class="form-inline" onsubmit="return false">
										<div class="form-group">
											名称：
											<input id="name" type="text" class="form-control" placeholder="名称">
											<button id="searchBt" class="layui-btn layui-btn-sm" permission="dict:query"><i class="layui-icon">&#xe615;</i>搜索</button>
										</div>
									</form>
	                   			</td>
	                   			<td align="right">
	                   				
	                   			</td>
	                   		</tr> 
	                    </table>
					</div>
                </header>
                
                <div>
                    <div class="widget-body no-padding">
                        <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                            <thead>
                                <tr>
                                </tr>                       
                                <tr>
									<th>服务名</th>
									<th>端口</th>
									<th>ip</th>
									<th>状态</th>
								    <th>操作</th>
								</tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

<script type="text/javascript" src="/js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="/js/jq.js"></script>
<script type="text/javascript" src="/js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="/layui/layui.js"></script>
<script type="text/javascript">

var example;
var pers = '';
function init(){
	example = 
    	$('#dt-table').DataTable({
        	"searching": false,
        	"processing": false,
        	"serverSide" : true,
        	"ordering": false,
        	"paging": false,
        	"ajax": {
        		"url" : "/clentInfo/list",
        		"type":"get",
        		"data":function(d){
    				d.type = $("#name").val();
    			},
    			"error":function(xhr, textStatus, errorThrown){
    				var msg = xhr.responseText;
    				console.log(msg);
    				var response = JSON.parse(msg);
    				var code = response.code;
    				var message = response.message;
    				if (code == 400) {
    					layer.msg(message);
    				} else if (code == 401) {
    					localStorage.removeItem("token");
    					layer.msg("token过期，请先登录", {shift: -1, time: 1000}, function(){
    						location.href = '/login.html';
                        });
    				} else if (code == 403) {
    					console.log("未授权:" + message);
    					layer.msg('未授权');
    				} else if (code == 500) {
    					layer.msg('系统错误：' + message);
    				}
    			}
        	},
        	"dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",	
            "columns": [
				{"data" : "name", "defaultContent" : ""},
				{"data" : "port", "defaultContent" : ""},
				{"data" : "host", "defaultContent" : ""},
				{"data": "status", "defaultContent": ""	},
				{ 
					"data": "", 
					"defaultContent": "",
					"orderable":false,
			        "render": function (data, type, row) {
             		           var id = row['id'];
             		           var href = "updatePro.html?id=" + id;
                             	 var up  = $("<button class='layui-btn layui-btn-xs' title='上线'  onclick='ops(\"" + row.host +"\","+ row.managementPort+","+1 +")'><i class='layui-icon'>&#xe652;</i></button>").prop("outerHTML");
                             	 var down  = $("<button class='layui-btn layui-btn-xs' title='下线'  onclick='ops(\"" + row.host +"\","+ row.managementPort+","+2 +")'><i class='layui-icon'>&#xe651;</i></button>").prop("outerHTML");
                             	 var stop  = $("<button class='layui-btn layui-btn-xs' title='停止'  onclick='ops(\"" + row.host +"\","+ row.managementPort+","+3 +")'><i class='layui-icon'>&#xe642;</i></button>").prop("outerHTML");
                             	 var restart  = $("<button class='layui-btn layui-btn-xs' title='重启'  onclick='ops(\"" + row.host +"\","+ row.managementPort+","+5 +")'><i class='layui-icon'>&#xe60f;</i></button>").prop("outerHTML");
                               	 var refresh  = $("<button class='layui-btn layui-btn-xs' title='刷新'  onclick='ops(\"" + row.host +"\","+ row.managementPort+","+4 +")'><i class='layui-icon'>&#xe669;</i></button>").prop("outerHTML");
       		            
                               return up + down+ stop+ restart+refresh;
               	            }		
				}
                
            ]
        } );
}

layui.use('layer', function(){
    var layer = layui.layer;
});

function ops(ip,port,ops){
	
	var req = {} ;
	req.ip = ip ;
	req.port = port ;
	req.operate = ops ;
	var str ;
	if(ops==1){
		str="上线";
	}else if(ops==2){
		str="下线";
	}else if(ops==3){
		str="停止";
	}else if(ops==4){
		str="刷新";
	}else if(ops==5){
		str="重启";
	}
	
    layer.confirm('确定要'+str+'吗？', {
        btn : [ '确定', '取消' ]
    }, function() {
        $.ajax({
            type : 'post',
            url : '/clentInfo/operate',
            data: JSON.stringify(req),
            dataType:"html",
            contentType: "application/json",
            success : function(data) {
            	example.ajax.reload();
                layer.msg(data.message);
            }
        });
        layer.close(1);
    });
}

$("#searchBt").click(function(){
	example.ajax.reload();
});

init();
</script>
